#pragma once

#include <agents.h>

class CWSAAsyncSelectModelLib :
	public INetworkLib
{
	enum 
	{
		WM_SOCKET = WM_USER + 1
	};
	ON_CONNECT_FUNCTOR		m_onConnectFunctor;
	ON_ACCEPT_FUNCTOR		m_onAcceptFunctor;	
	ON_DISCONNECT_FUNCTOR	m_onDisconnectFunctor;
	ON_RECV_FUNCTOR			m_onRecvFunctor;

	HINSTANCE				m_hInstance;			// current instance
	std::tstring			m_windowTitle;			// The title bar text
	std::tstring			m_windowClass;			// the main window class name
	HWND					m_hMainWnd;
	Concurrency::event		m_waitForCreatingWindow;
	IAsyncSendQueue*		m_pAsyncSendQueue;


	ATOM					registerWindowClass(HINSTANCE hInstance);
	BOOL					initWindow(HINSTANCE hInstance, int nCmdShow);
	static LRESULT CALLBACK wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
	static UINT __stdcall	windowWorkerThread( void* pArguments );
	bool					Send(SOCKET socket);
public:
	CWSAAsyncSelectModelLib();
	virtual ~CWSAAsyncSelectModelLib(void);

	virtual bool	Initialize(ON_CONNECT_FUNCTOR onConnect
							, ON_ACCEPT_FUNCTOR onAccept
							, ON_DISCONNECT_FUNCTOR onDisconnect
							, ON_RECV_FUNCTOR onRecv		
							) override;

	virtual bool	Connect(const std::tstring& ip, WORD port) override;

	virtual bool	Send(SOCKET socket, IStreamBuffer& stream) override;

	virtual bool	Disconnect(SOCKET socket) override;

	virtual bool	Listen(const std::tstring& ip, WORD port, int backlog) override;

	virtual void	Release() override;
};

